# -*- coding:utf-8 -*-
import logging
import time
import os.path

class Log:
    def __init__(self):
        self.logger = logging.getLogger()
        self.logger.setLevel('DEBUG')

        log_path = os.path.dirname(
            os.path.dirname(os.path.abspath(__file__))) + os.sep + "result" + os.sep + "auto-test_%s.log" % time.strftime("%Y-%m-%d", time.localtime())
        self.handler = logging.FileHandler(log_path)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s : %(message)s')
        self.handler.setFormatter(formatter)
        if not self.logger.handlers:      #增加判断，不然会造成重复日志
            self.logger.addHandler(self.handler)

    def log(self,level,message):
        if level == 'DEBUG':
            self.logger.debug(message)
        if level == 'INFO':
            self.logger.info(message)
        if level == 'WARNING':
            self.logger.warning(message)
        if level == 'ERROR':
            self.logger.error(message)
        if level == 'CRITICAL':
            self.logger.critical(message)
        #self.logger.removeHandler(self.handler)

if __name__ == '__main__':
    log = Log();
    log.log('INFO',"add")
